package com.nehc.nettyserver.common.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 消息实体类
 * 
 * @author NEHC
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message {
    /**
     * 消息类型枚举
     */
    public enum Type {
        TEXT("text"),           // 文本消息
        IMAGE("image"),         // 图片消息
        FILE("file"),          // 文件消息
        SYSTEM("system"),      // 系统消息
        HEARTBEAT("heartbeat"), // 心跳消息
        NOTIFICATION("notification"), // 通知消息
        GROUP("group"),        // 群组消息
        PRIVATE("private");    // 私聊消息

        private final String value;

        Type(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public static Type fromValue(String value) {
            for (Type type : values()) {
                if (type.value.equals(value)) {
                    return type;
                }
            }
            return TEXT; // 默认返回文本类型
        }
    }

    /**
     * 消息ID
     */
    private String id;
    
    /**
     * 消息类型
     */
    private Type type;
    
    /**
     * 发送者ID
     */
    private String senderId;
    
    /**
     * 接收者ID
     */
    private String receiverId;
    
    /**
     * 消息时间戳
     */
    private long timestamp;
    
    /**
     * 消息负载
     */
    private Map<String, Object> payload;
    
    /**
     * 创建一个新消息
     */
    public Message(Type type, String senderId, String receiverId, Map<String, Object> payload) {
        this.id = UUID.randomUUID().toString();
        this.type = type;
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.timestamp = System.currentTimeMillis();
        this.payload = payload != null ? payload : new HashMap<>();
    }
    
    /**
     * 创建一个系统消息
     */
    public static Message createSystemMessage(String content) {
        Map<String, Object> payload = new HashMap<>();
        payload.put("content", content);
        
        Message message = new Message();
        message.id = UUID.randomUUID().toString();
        message.type = Type.SYSTEM;
        message.senderId = "system";
        message.timestamp = System.currentTimeMillis();
        message.payload = payload;
        
        return message;
    }
    
    /**
     * 创建一个心跳消息
     */
    public static Message createHeartbeatMessage(String clientId) {
        Map<String, Object> payload = new HashMap<>();
        payload.put("timestamp", System.currentTimeMillis());
        
        Message message = new Message();
        message.id = UUID.randomUUID().toString();
        message.type = Type.HEARTBEAT;
        message.senderId = clientId;
        message.receiverId = "server";
        message.timestamp = System.currentTimeMillis();
        message.payload = payload;
        
        return message;
    }
    
    /**
     * 添加负载项
     */
    public void addPayload(String key, Object value) {
        if (this.payload == null) {
            this.payload = new HashMap<>();
        }
        this.payload.put(key, value);
    }
    
    /**
     * 获取负载项
     */
    public Object getPayload(String key) {
        return this.payload != null ? this.payload.get(key) : null;
    }
    
    /**
     * 获取消息内容
     */
    public String getContent() {
        return (String) getPayload("content");
    }
    
    /**
     * 设置消息内容
     */
    public void setContent(String content) {
        addPayload("content", content);
    }
} 